main_file_name ) ) { return; } $content = self::get_asset_file_content( $ext ); // If no premium elements exist on the page, then don't generate files if ( empty( $content ) ) { return; } if ( 'css' === $ext && is_rtl() ) { $rtl_file_name = Helper_Functions::get_safe_path( PREMIUM_ASSETS_PATH . '/pa-frontend-rtl-' . self::$post_id . '.min.css' ); } if ( ! file_exists( PREMIUM_ASSETS_PATH ) ) { wp_mkdir_p( PREMIUM_ASSETS_PATH ); } if ( 'css' === $ext ) { if ( is_rtl() ) { // Make sure to delete the file before creating the new one. file_put_contents( $rtl_file_name, '@charset "UTF-8";' . $content['rtl'] ); // phpcs:ignore } else { file_put_contents( $main_file_name, '@charset "UTF-8";' . $content['main'] ); // phpcs:ignore } } else { file_put_contents( $main_file_name, $content ); // phpcs:ignore } } /** * Clear cached file. * Delete file if it exists. * * @access public * @since 4.6.1 * * @param string $file_name file name. */ public static function clear_cached_file( $file_name ) { if ( file_exists( $file_name ) ) { unlink( $file_name ); } } /** * Remove files * * @since 4.6.1 */ public static function remove_files() { $ext = array( 'css', 'js' ); foreach ( $ext as $e ) { $path = PREMIUM_ASSETS_PATH . '/pa-frontend-' . self::$post_id . '.min.' . $e; if ( 'css' === $e ) { $rtl_path = PREMIUM_ASSETS_PATH . '/pa-frontend-rtl-' . self::$post_id . '.min.' . $e; self::clear_cached_file( $rtl_path ); } self::clear_cached_file( $path ); } } /** * Get Asset File Content. * * Collects pa/papro widgets assets. * * @access public * @since 4.6.1 * * @param string $ext js|css. * * @return string|array $content */ public static function get_asset_file_content( $ext ) { // Get the cached elements of the current post/page. $pa_elements = get_option( 'pa_elements_' . self::$post_id, array() ); if ( empty( $pa_elements ) ) { return ''; } $content = ''; if ( 'css' === $ext ) { $rtl_content = ''; } $pa_elements = self::prepare_pa_elements( $pa_elements, $ext ); foreach ( $pa_elements as $element ) { $path = self::get_file_path( $element, $ext ); if ( ! $path ) { continue; } $content .= self::get_file_content( $path ); if ( 'css' === $ext && is_rtl() ) { $rtl_path = self::get_file_path( $element, $ext, '-rtl' ); $rtl_content .= self::get_file_content( $rtl_path ); } } if ( 'css' === $ext ) { $content = array( 'main' => $content, 'rtl' => $rtl_content, ); // Fix: at-rule or selector expected css error. $content = str_replace( '@charset "UTF-8";', '', $content ); } return $content; } /** * Prepare PA Elements. * * @access public * @since 4.6.1 * * @param array $elements post elements. * @param string $ext js|css. * * @return array */ public static function prepare_pa_elements( $elements, $ext ) { if ( 'css' === $ext ) { $common_assets = self::has_free_elements( $elements ) ? array( 'common' ) : array(); $common_assets = self::has_pro_elements( $elements ) ? array_merge( $common_assets, array( 'common-pro' ) ) : $common_assets; $elements = array_merge( $elements, $common_assets ); } else { $indep_elements = array( 'social-common', 'premium-hscroll', 'premium-lottie', 'premium-vscroll', 'premium-nav-menu', 'premium-addon-maps', 'premium-woo-products-pro', 'premium-addon-testimonials', 'premium-addon-pricing-table', 'premium-addon-image-separator', ); $elements = array_diff( $elements, $indep_elements ); } return $elements; } /** * Get File Content. * * @param string $path file path. * * @return string */ public static function get_file_content( $path ) { $file_content = rplg_urlopen( $path ); if ( isset( $file_content['code'] ) ) { if ( in_array( $file_content['code'], array( 404, 401 ), true ) ) { return ''; } } return self::clean_content( $file_content['data'] ); } /** * Clean Content * Removes Page Html if it's returned as result. * * @param string $content file content. * * @return string */ public static function clean_content( $content ) { if ( strpos( $content, '' ) ) { $content = explode( '', $content )[0]; } if ( strpos( $content, '' ) ) { $content = explode( '', $content )[0]; } return $content; } /** * Get File Path. * Construct file path. * * @param string $element pa element name. * @param string $ext file extension ( js|css). * @param string $dir post dir (-rtl|''). * * @return string file path. */ public static function get_file_path( $element, $ext, $dir = '' ) { $is_pro = self::is_pro_widget( $element ); $papro_activated = apply_filters( 'papro_activated', false ) && version_compare( PREMIUM_PRO_ADDONS_VERSION, '2.7.1', '>' ); if ( ! $papro_activated && $is_pro ) { return false; } $element = str_replace( '-addon', '', $element ); $path = $is_pro ? PREMIUM_PRO_ADDONS_URL : PREMIUM_ADDONS_URL; return $path . 'assets/frontend/min-' . $ext . '/' . $element . $dir . '.min.' . $ext; } /** * Is Pro Widget. * Checks if the widget is pro. * * @access public * @since 4.6.1 * * @param string $widget widget name. * * @return bool */ public static function is_pro_widget( $widget ) { $pro_names = array_merge( array( 'common-pro', 'premium-reviews', 'premium-woo-products-pro', 'social-common' ), self::get_pro_widgets_names() ); return in_array( $widget, $pro_names, true ); } /** * Has Pro Elements. * Check if the post has pa pro elements. * * @access public * @since 4.6.1 * * @param array $post_elems post elements. * * @return boolean */ public static function has_pro_elements( $post_elems ) { $papro_elems = self::get_pro_widgets_names(); $has_pro = array_intersect( $post_elems, $papro_elems ) ? true : false; return $has_pro; } /** * Has Free Elements. * Check if the post has pa elements. * * @access public * @since 4.6.1 * * @param array $post_elems post elements. * * @return boolean */ public static function has_free_elements( $post_elems ) { $pa_elems = Admin_Helper::get_free_widgets_names(); $has_free = array_intersect( $post_elems, $pa_elems ) ? true : false; return $has_free; } /** * Get Pro Widgets Names. * * @access public * @since 4.6.1 * * @return array */ public static function get_pro_widgets_names() { $pro_elems = Admin_Helper::get_pro_elements(); $pro_names = array(); foreach ( $pro_elems as $element ) { if ( isset( $element['name'] ) ) { array_push( $pro_names, $element['name'] ); } } return $pro_names; } /** * Creates and returns an instance of the class. * * @since 4.6.1 * @access public * * @return object */ public static function get_instance() { if ( ! isset( self::$instance ) ) { self::$instance = new self(); } return self::$instance; } }